home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14687 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: watnews.watson.ibm.com!ncohen
  2. From: ncohen@watson.ibm.com (Norman H. Cohen)
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: some questions re. Ada/GNAT from a C++/GCC user
  5. Date: 1 Apr 1996 17:36:33 GMT
  6. Organization: IBM T.J. Watson Research Center
  7. Distribution: world
  8. Message-ID: <4jp471$17vn@watnews1.watson.ibm.com>
  9. References: <wnewmanDoxrCp.DKv@netcom.com> <4je9ju$174r@watnews1.watson.ibm.com> <ROGOFF.96Mar28134118@sccm.Stanford.EDU> <4jhe1v$m0g@dayuc.dayton.saic.com> <Dp3G4u.KEA@world.std.com> <dewar.828333135@schonberg>
  10. Reply-To: ncohen@watson.ibm.com
  11. NNTP-Posting-Host: rios8.watson.ibm.com
  12.  
  13. In article <dewar.828333135@schonberg>, dewar@cs.nyu.edu (Robert Dewar)
  14. writes: 
  15. |> The trouble with mixing declarations and statements is that it blurs
  16. |> the lines beween elaboration and execution,
  17.  
  18. These lines are already blurred in Ada, because elaboration takes place
  19. at run time, when the flow of control reaches a declaration.  Indeed,
  20. RM95 3.1(11) says, "The process by which a cosntruct achieves its
  21. run-time effect is called _execution_.  One of the terms execution,
  22. elaboration, or evaluation is defined by this International Standard for
  23. each construct that has a run-time effect." In other words, elaboration
  24. is one kind of execution.  There was no such passage in RM83, resulting
  25. in the need for such circumlocutions as "The foregoing is expressed in
  26. terms of the process that is called execution; it applies equally to the
  27. processes that are called evaluation and elaboration" (RM83 1.6(9)).
  28.  
  29. |>                                             and can result in considerable
  30. |> semantic confusion. Where for example would tasks be activated, and
  31. |> what does
  32. |>
  33. |>   if x then a : integer; ....
  34. |>
  35. |> Yes, this can be given a meaning, but I don't think it is worth the
  36. |> effort.
  37.  
  38. Rather than allowing an aribtrary interleaving of declarations and
  39. statements as in C++, we can simply replace the rule
  40.  
  41.    sequence_of_statements ::= statement {statement}
  42.  
  43. with
  44.  
  45.    sequence_of_statements ::=
  46.       {declarative_item} statement {statement}
  47.  
  48. and add the rule that the execution of a sequence of statements consists
  49. of the elaboration of its declarative part followed by the execution of
  50. its statements.  In other words,
  51.  
  52.    if P then
  53.       X: constant Integer := ...;
  54.       ...
  55.       Y := X;
  56.    else
  57.       X: constant Integer := ...;
  58.       ...
  59.       Z:= X;
  60.    end if;
  61.  
  62. becomes just a lightweight equivalent of
  63.  
  64.    if P then
  65.       declare
  66.          X: constant Integer := ...;
  67.       begin
  68.          ...
  69.          Y := X;
  70.       end
  71.    else
  72.       declare
  73.          X: constant Integer := ...;
  74.       begin
  75.          ...
  76.          Z:= X;
  77.       end;
  78.    end if;
  79.  
  80. (Note that if you want exception handlers inside the if statement, an
  81. explicit block statement is still required.)
  82.  
  83. |>         Having programmed in Algol-68 a lot, this is one A68 feature
  84. |> I can do without.
  85.  
  86. Actually, this is one aspect of C that I actually LIKE.  I always use {
  87. and } brackets for my C compound statements to avoid accidently changing
  88.  
  89.    if (P)
  90.       w = x;
  91.  
  92. to
  93.  
  94.    if (P)
  95.       w = x;
  96.       y = z;    /* Oh, dear.  Fooled by the indentation */
  97.  
  98. later.  Thus, at no extra notational cost, if a variable is to be used
  99. only within one branch of an if statement, for example, I can declare it
  100. locally to that if statement.  Besides making the code more readable by
  101. bringing declarations textually closer to statements that use them, this
  102. makes it much easier to move chunks of code around, e.g. to move part of
  103. one function definition that has grown too large into a function
  104. definition of its own, because a chunk of text becomes more independent
  105. of the surrounding context.
  106.  
  107. I don't find myself doing the same thing (with block statements) in Ada
  108. because the notation is too heavy (especially when set in bolface ;-) ).
  109. For me at least, the lack of a shorthand equivalent is a psychological
  110. barrier to localizing declarations.  I believe that if the shorthand were
  111. allowed, many programmers would write more readable Ada code.
  112.  
  113. --
  114. Norman H. Cohen    ncohen@watson.ibm.com
  115.